package com.netty.websocket;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class WSServer {

//    public static void main(String[] args) throws Exception {
//
//        SpringApplication.run(NettyApplication.class, args);
//
//        NioEventLoopGroup mainGroup = new NioEventLoopGroup();
//        NioEventLoopGroup subGroup = new NioEventLoopGroup();
//
//        try {
//            ServerBootstrap server = new ServerBootstrap();
//            server.group(mainGroup, subGroup)
//                    .channel(NioServerSocketChannel.class)
//                    .childHandler(new WSServerInitialzer());
//
//            ChannelFuture future = server.bind(7078).sync();
//            future.channel().closeFuture().sync();
//        } finally {
//            mainGroup.shutdownGracefully();
//            subGroup.shutdownGracefully();
//        }
//    }


    private static class SingletionWSServer {
        static final WSServer instance = new WSServer();
    }

    public static WSServer getInstance() {
        return SingletionWSServer.instance;
    }

    private EventLoopGroup mainGroup;
    private EventLoopGroup subGroup;
    private ServerBootstrap server;
    private ChannelFuture future;

    public WSServer() {
        mainGroup = new NioEventLoopGroup();
        subGroup = new NioEventLoopGroup();
        server = new ServerBootstrap();
        server.group(mainGroup, subGroup).channel(NioServerSocketChannel.class).childHandler(new WSServerInitialzer());
    }

    public void start() {
        this.future = server.bind(7078);
        log.info("netty websocker server 启动完毕。。。。");
    }

}
